home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 4 code / Perils of PS II / Perils of PS II / Tips2.p < prev    next >
Encoding:
Text File  |  1990-08-14  |  6.9 KB  |  178 lines  |  [TEXT/MPS ]

  1. PROGRAM Tips1;
  2.  
  3. USES Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf,MacPrint;
  4.  
  5. CONST
  6.     PostScriptBegin =         190;
  7.     PostScriptEnd =            191;
  8.     PostScriptHandle =        192;
  9.     PostScriptBeginNoSave =    196;
  10.  
  11.     PROCEDURE SendPostScript(theComment: Str255);
  12.     VAR
  13.         PSCommand    : Str255;
  14.         CommandHdl    : Handle;
  15.         CRString    : Str255;
  16.         theError    : OSErr;
  17.     BEGIN
  18.         CRString := ' ';
  19.         CRString[1] := CHR(13);
  20.         PSCommand := theComment;
  21.         PSCommand := CONCAT(PSCommand, CRString);
  22.         theError := PtrToHand(POINTER(ORD(@PSCommand) + 1), CommandHdl, 
  23.                     LENGTH(PSCommand));
  24.         if theError <> noErr THEN BEGIN
  25.             (* Handle the error! *)
  26.         END;
  27.         PicComment(PostScriptHandle, LENGTH(PSCommand), CommandHdl);
  28.         DisposHandle(CommandHdl);
  29.     END;
  30.  
  31.     PROCEDURE SetFont(fontName: Str255; fontSize: INTEGER; fontStyle: Style);
  32.     VAR
  33.         theFontID:  INTEGER;
  34.         thePenLoc:  Point;
  35.     BEGIN        
  36.         GetFNum(fontName, theFontID);       (* Get the font ID.                         *)
  37.         TextFont(theFontID);                (* Set it.                                  *)
  38.         TextSize(fontSize);                 (* Set the size...                          *)
  39.         TextFace(fontStyle);                (* ...and the style.                        *)
  40.         GetPen(thePenLoc);                  (* Save the current pen position.           *)
  41.         DrawChar(' ');                      (* Draw a space so the font gets downloaded.*)
  42.         MoveTo(thePenLoc.h, thePenLoc.v);   (* Restore the original pen position.       *)
  43.     END;
  44.  
  45. PROCEDURE DrawStuff(theWorld : Rect);
  46.     { Draw whatever you want here.  Make sure it fits in the world rectangle. }
  47. BEGIN
  48.     (* First, let's define a PostScript dictionary.  Since we aren't actually    *)
  49.     (* drawing anything, we don't need to turn off Quickdraw (ie. we don't need    *)
  50.     (* the PostScriptBegin/End comments).                                        *)
  51.     (* First create a dictionary that can hold 10 objects. *)
  52.     SendPostScript('/mydict 10 dict def');
  53.     (* Now make it current.  All implicit operations from now on will go to    *)
  54.     (* mydict.                                                                *)
  55.     SendPostScript('mydict begin');
  56.     (* Now define the routine to save the definition of bu on the stack, and*)
  57.     (* then define it to do nothing.                                        *)
  58.     SendPostScript('/killbu {//md /bu get //md /bu {} put} def');
  59.     (* Now define a routine to pop the old definition of bu off the stack,    *)
  60.     (* and back into the bu symbol.                                            *)
  61.     SendPostScript('/restorebu {//md exch /bu exch put} def');
  62.     (* Now do the same for bn.                                                *)
  63.     SendPostScript('/killbn {//md /bn get //md /bn {} put} def');
  64.     SendPostScript('/restorebn {//md exch /bn exch put} def');
  65.     (* Now define a test routine that we can execute to see if our dict was    *)
  66.     (* truly preserved.                                                        *)
  67.     SendPostScript('/titleshow {dup gsave');
  68.     SendPostScript('currentscreen 3 -1 roll pop 120 3 1 roll setscreen');
  69.     SendPostScript('.5 setgray show grestore true charpath gsave');
  70.     SendPostScript('1 setlinewidth 0 setgray stroke grestore');
  71.     SendPostScript('.5 setlinewidth 1 setgray stroke }def');
  72.     (* This ends the 'mydict begin' above, restoring md as the current dict.*)
  73.     SendPostScript('end');
  74.  
  75.     (* Okay, now we have our dictionary defined, we need to kill off those    *)
  76.     (* nasty bn and bu operators before they kill us (ie. before a font     *)
  77.     (* download).                                                            *)
  78.     (* First point to our dictionary.                                        *)
  79.     SendPostScript('mydict begin');
  80.     (* Now kill off bu, saving its original definition on the stack.        *)
  81.     SendPostScript('//md /bu known {killbu} if');
  82.     (* ...and the same for bn.                                                *)
  83.     SendPostScript('//md /bn known {killbn} if');
  84.     (*************************** IMPORTANT **********************************)
  85.     (* Since the definition of bu and bn have been saved on the stack, they    *)
  86.     (* must be restored in the opposite order that they were killed.  In     *)
  87.     (* this case, bu was killed first, so restorebn must be called before    *)
  88.     (* restorebu.  If not, the routines will be reversed, and you will get    *)
  89.     (* a limitcheck (out of memory) error in short order.                    *)
  90.     (*************************** IMPORTANT **********************************)
  91.     SendPostScript('end');
  92.  
  93.     (* Set the font using our new SetFont routine.  This will set the font     *)
  94.     (* for both the Quickdraw and PostScript worlds.  Since we have killed    *)
  95.     (* bn and bu, this should have no effect on our PostScript dictionary.    *)
  96.     SetFont('Times', 50, [bold]);
  97.  
  98.     PicComment(PostScriptBegin, 0, NIL);  
  99.         (********************************************)
  100.         (*** Quickdraw representation of graphic. ***)
  101.         (********************************************)
  102.         (* These calls are only executed by Quickdraw (i.e. non-PostScript)    *)
  103.         (* devices.                                                            *)
  104.         MoveTo(100, 100);
  105.         DrawString('UnFancy Title');
  106.         
  107.         (*********************************************)
  108.         (*** PostScript representation of graphic. ***)
  109.         (*********************************************)
  110.         SendPostScript('mydict begin');        (* Point to ours.            *)
  111.         SendPostScript('100 100 moveto (Fancy Title) titleshow');                (* Execute test.            *)
  112.         SendPostScript('end');                (* Reset back to last dict.    *)
  113.     PicComment(PostScriptEnd, 0, NIL);
  114.     
  115.     (* Now we're all done with our job, so to be polite, we restore the    *)
  116.     (* original definitions of bn and bu.  REMEMBER that the restorexx    *)
  117.     (* routines must be executed in the opposite order that the killxx    *)
  118.     (* routines were.                                                    *)
  119.     SendPostScript('mydict begin');
  120.     SendPostScript('//md /bn known {restorebn} if');
  121.     SendPostScript('//md /bu known {restorebu} if');
  122.     SendPostScript('end');
  123. END;
  124.  
  125.  
  126. PROCEDURE PrintStuff;
  127. VAR
  128.     thePrRec    : THPrint;
  129.     thePrPort   : TPPrPort;
  130.     theStatus   : TPrStatus;
  131.     oldPort     : GrafPtr;
  132.     theError    : OSErr;
  133.     theVers:    INTEGER;
  134.  
  135. BEGIN
  136.     GetPort(oldPort);
  137.     thePrRec := THPrint(NewHandle(SIZEOF(TPrint)));
  138.    
  139.     PrOpen;
  140.     theVers := PrDrvrVers;
  141.     IF PrError = noErr THEN BEGIN
  142.        PrintDefault(thePrRec);
  143.            IF NOT PrStlDialog(thePrRec) THEN
  144.                ExitToShell;
  145.            IF NOT PrJobDialog(thePrRec) THEN
  146.                ExitToShell;
  147.            thePrPort := PrOpenDoc(thePrRec, NIL, NIL);
  148.            IF PrError = noErr THEN BEGIN
  149.                   PrOpenPage(thePrPort, NIL);
  150.                IF PrError = noErr THEN BEGIN
  151.                
  152.                    DrawStuff(thePrRec^^.prInfo.rPage);
  153.                    
  154.                END;
  155.               PrClosePage(thePrPort)
  156.            END;
  157.        PrCloseDoc(thePrPort);
  158.        IF (thePrRec^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  159.            PrPicFile(thePrRec, NIL, NIL, NIL, theStatus);
  160.     END;                
  161.     PrClose;
  162.  
  163.     SetPort(oldPort);
  164. END;
  165.  
  166. BEGIN
  167.     InitGraf(@thePort);                  {initialize QuickDraw}
  168.     InitFonts;                                    {initialize Font Manager}
  169.     FlushEvents(everyEvent, 0); {call OS Event Mgr to discard any previous events}
  170.     InitWindows;                               {initialize Window Manager}
  171.     InitMenus;                                    {initialize Menu Manager}
  172.     TEInit;                                       {initialize TextEdit}
  173.     InitDialogs(NIL);                       {initialize Dialog Manager}
  174.     InitCursor;                                {call QuickDraw to make cursor (pointer) an arrow}
  175.  
  176.     PrintStuff;
  177. END.
  178.